use std::vec::Vec;
use std::cmp::Ordering;
use std::io;

#[derive(Default)]
pub struct Tracer {
    data: Vec<u8>,
}

impl Tracer {
    pub fn reset(mut self) -> Self {
        self.data.clear();
        self
    }
    pub fn record(mut self, buf: &[u8]) -> Self {
        let _ = io::Write::write(&mut self, buf).unwrap();
        self
    }






    pub fn data(&self) -> &[u8] {
        self.data.as_slice()
    }
    pub fn len(&self) -> usize {
        self.data.len()
    }
    pub fn is_empty(&self) -> bool {
        self.data.len() == 0
    }

    pub fn equal_to(&self, bytes: &[u8]) -> bool {
        self.data.iter().cmp(bytes.iter()) == Ordering::Equal
    }

}

impl io::Write for Tracer {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.data.extend_from_slice(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::io::Write;
    #[test]
    fn use_tracer(){
        let target = b"hello";
        {
            let mut t = Tracer::default();
            let _ = t.write(target);
            //println!("use_tracer: {:?}", t.data());
            assert!(t.equal_to(target));
        }

        assert!(Tracer::default().record(target).equal_to(target));

        assert!(Tracer::default()
            .record(target)
            .record(target)
            .equal_to(b"hellohello"));


    }
}

